/**
 * Created by liubin on 2014/11/26.
 */
define(['user/module', 'lodash'], function (module, _) {

    'use strict';

    module.registerController('UserListCtrl', function($scope, $state, $stateParams, $log, DTOptionsBuilder, DTColumnBuilder, PageSize,
       UserList, User, DTLoadingTemplate, AlertService, Tools){
        $log.log('UserListCtrl');
        $log.log(DTLoadingTemplate);
        //$scope.table = null;
        $scope.dtOptions = DTOptionsBuilder.newOptions().
            withOption('dom',
            // 追加按钮行
            //'<"toolbar">rtip'
        "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'>r>" +
                        "t" +
                        "<'dt-toolbar-footer'<'col-sm-6 col-xs-2 hidden-xs'<'add-btn'>><'col-xs-12 col-sm-6'p>>"
        ).withOption('ajax', function(data, callback, settings){
                // 数据读取
                var users = [];
                $log.log(data);
                var dir = data.order[0].dir || '';
                var order = data.columns[data.order[0].column].data || ''
                UserList.get(
                    {
                        draw: data.draw,
                        size: PageSize,
                        start: data.start,
                        // 查询
                        query: data.search.value,
                        order: order,
                        dir: dir
                    },
                    {},  function (result){
                    angular.forEach(result.data, function (value, key) {
                        value.op = '';
                        users.push(value);
                    });
                    callback({
                        draw: result.draw,
                        data: users,
                        recordsTotal: result.total,
                        recordsFiltered: result.total
                    });
                });
                //todo: add user get failure
            }).
            withOption('rowCallback', function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                // 增加按钮事件
                // 查看详细
                $('a[action=view]', nRow).bind('click', function(){
                    $scope.$apply(function() {
                        $scope.view(aData);
                    });
                });
                // 删除
                $('a[action=delete]', nRow).bind('click', function(){
                    $scope.$apply(function() {
                        $scope.delete(aData);
                    });
                });
                if($('a[action=view]', nRow)){
                    $('a[action=view]', nRow).tooltip();
                }
                if($('a[action=delete]', nRow)){
                    $('a[action=delete]', nRow).tooltip();

                }
                return nRow;
            }).
            withDataProp('data').
            withOption('serverSide', true).
            withPaginationType('full_numbers');
        $scope.handleSearch = function(e){
            if (e.which === 13) {
                //if($scope.table){
                //    console.log('DoSearch!!!');
                //    $scope.table.DataTable().ajax.reload();
                //}
            }
        };
        $scope.dtColumns = [
            DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
            DTColumnBuilder.newColumn('name').withTitle('用户名'),
            DTColumnBuilder.newColumn('real_name').withTitle('姓名'),
            DTColumnBuilder.newColumn('op').withTitle('操作').notSortable().renderWith(
                function(){
                    //return actionColumnTemplate;
                    return '<a href="javascript:" action="view" class="txt-color-blue" title="查看" style="margin:0px 5px 0px 0px"><i class="fa fa-search-plus fa-lg"></i></a><a href="javascript:" action="delete" class="txt-color-red" title="删除" style="margin:0px 5px 0px 0px"><i class="fa fa-trash-o fa-lg"></i></a>';
                }
            )
        ];
        $scope.$on('event:dataTableLoaded', function(event, loadedDT) {
            // 追加按钮
            $("div.add-btn").html('<a class="btn btn-danger" style="margin:0px 0px 0px 0px">新增</a>');
            $("div.add-btn a").bind('click', function(){
                $scope.$apply(function() {
                    $state.go('app.user.add');
                });
            });
        });
        $scope.view = function(data){
            $log.log('view');
            $state.go('app.user.view', {id: data.id});
        }
        $scope.delete = function(data){
            $log.log('delete');
            AlertService.confirm('删除', '确认要删除该条记录?', function(){
                $log.log(data);
                User.delete({
                    id: data.id
                },{},function(result){
                    // 刷新状态
                    Tools.reloadState();
                });
            });
        }
    });

});